/**
 * 
 */
package com.googolmo.NowPlaying;

import java.util.Date;
import java.util.List;

import com.googolmo.NowPlaying.Receiver.MusicReceiver;
import com.googolmo.NowPlaying.SharePreferences.MusicPreferences;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

/**
 * @author googolmo
 * 
 */
public class NPUtils {

	/**
	 * 是否开启NowPlaying
	 * 
	 * @param context
	 * @param enable
	 */
	public static void enableNP(Context context, boolean enable) {
		MusicPreferences.setIsEnabled(context, enable);

		PackageManager pManager = context.getPackageManager();
		ComponentName componentName = new ComponentName(context,
				MusicReceiver.class);

		if (enable) {
			Log.i(Constant.TAG, "NowPling is enabled");
			pManager.setComponentEnabledSetting(componentName,
					PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
					PackageManager.DONT_KILL_APP);
		} else {
			Log.i(Constant.TAG, "NowPling is disabled");
			pManager.setComponentEnabledSetting(componentName,
					PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
					PackageManager.DONT_KILL_APP);

		}
	}

	/**
	 * 服务是否运行
	 * 
	 * @param context
	 * @param className
	 * @return
	 */
	public static boolean isServiceRunning(Context context, String className) {
		boolean isRunning = false;
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningServiceInfo> serviceList = activityManager
				.getRunningServices(Integer.MAX_VALUE);
		if (serviceList.size() <= 0) {
			isRunning = false;
		} else {
			for (int i = 0; i < serviceList.size(); i++) {
				if (serviceList.get(i).service.getClassName().equals(className)) {
					isRunning = true;
					break;
				}
			}
		}
		Log.d(Constant.TAG, "service is running=" + isRunning);
		return isRunning;
	}

	/**
	 * 
	 * @param date
	 *            开始时间
	 * @param date2
	 *            当前时间
	 * @return 两个时间点之间的差，单位毫秒
	 */
	public static long compareTime(Date begin, Date end) {
		return end.getTime() - begin.getTime();
	}

}
